package com.un.web.topic.service.impl;

import cn.hutool.db.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.un.core.common.Constants;
import com.un.core.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.un.web.topic.domian.entity.TopicType;
import com.un.web.topic.mapper.TopicTypeMapper;
import com.un.web.topic.service.TopicTypeService;

/**
 * @Author xingjian
 * @Date 2023-01-31
 **/

@Service
public class TopicTypeServiceImpl extends ServiceImpl<TopicTypeMapper, TopicType> implements TopicTypeService {

    @Autowired
    private TopicTypeMapper topicTypeMapper;

    @Override
    public void insertTopicType(TopicType topicType) {
        Long count = topicTypeMapper.selectCount(new QueryWrapper<TopicType>().eq("name", topicType.getName()));
        if (count>0){
            throw  new ServiceException(Constants.CODE_405,"当前题目类型已存在");
        }
        topicTypeMapper.insert(topicType);
    }

    @Override
    public PageInfo<TopicType> selectAllByPage(Page page) {
        PageHelper.startPage(page.getPageNumber(),page.getPageSize());
        List<TopicType> topicTypes = topicTypeMapper.selectList(null);
        return new PageInfo<>(topicTypes);
    }
}

